home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP12.TXT < prev    next >
Text File  |  1990-08-08  |  27KB  |  653 lines

  1.  
  2.                       Chapter 12 - Dynamic Allocation
  3.  
  4.  
  5.                         WHAT IS DYNAMIC ALLOCATION?
  6.  
  7.              Dynamic allocation is very intimidating to a person the
  8.         first time he comes across it, but that need not be.  Simply
  9.         relax  and  read this chapter carefully and you will have  a
  10.         good grounding in a very valuable programming resource.  All
  11.         of the variables in every program up to this point have been
  12.         static  variables as far as we  are  concerned.   (Actually,
  13.         some  of  them  have been "automatic" and  were  dynamically
  14.         allocated for you by the system,  but it was transparent  to
  15.         you.)   In  this  chapter,  we will study  some  dynamically
  16.         allocated  variables.  They are variables that do not  exist
  17.         when  the program is loaded, but are created dynamically  as
  18.         they are needed.  It is possible, using these techniques, to
  19.         create as many variables as needed, use them, and deallocate
  20.         their space for use by other variables.  As usual, the  best
  21.         teacher is an example, so load and display the program named
  22.         DYNLIST.C.
  23.  
  24.              We  begin by defining a named structure "animal" with a
  25.         few  fields  pertaining  to dogs.   We  do  not  define  any
  26.         variables of this type,  only three pointers.  If you search
  27.         through  the  remainder  of the program,  you will  find  no
  28.         variables defined so we have nothing to store data in.   All
  29.         we have to work with are three pointers, each of which point
  30.         to the defined structure.   In order to do anything, we need
  31.         some variables, so we will create some dynamically.
  32.  
  33.                          DYNAMIC VARIABLE CREATION
  34.  
  35.              The first program statement, which assigns something to
  36.         the   pointer  "pet1"  will  create  a   dynamic   structure
  37.         containing  three variables.   The heart of the statement is
  38.         the "malloc" function buried in the middle of the statement.
  39.         This  is a "memory allocate" function that needs  the  other
  40.         things to completely define it.   The "malloc" function,  by
  41.         default, will allocate a piece of memory on a "heap" that is
  42.         "n" characters in length and will be of type character.  The
  43.         "n"  must be specified as the only argument to the function.
  44.         We will discuss "n" shortly,  but first we need to define  a
  45.         "heap".
  46.  
  47.                               WHAT IS A HEAP?
  48.  
  49.              Every compiler has a set of limitations on it as to how
  50.         big  the executable file can be,  how many variables can  be
  51.         used,  how long the source file can be, etc.  One limitation
  52.         placed  on users by the Turbo C compiler is a limit  of  64K
  53.         for  the  executable code if you happen to be in  the  small
  54.         memory   model.    This  is  because  the  IBM-PC   uses   a
  55.         microprocessor  with  a 64K segment size,  and  it  requires
  56.  
  57.  
  58.                                   Page 87
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 12 - Dynamic Allocation
  69.  
  70.  
  71.         special  calls to use data outside of a single segment.   In
  72.         order  to keep the program small and efficient, these  calls
  73.         are  not  used, and the memory space is  limited  but  still
  74.         adequate for most programs.
  75.  
  76.              In  this  model Turbo C defines two  heaps,  the  first
  77.         being called a "heap", and the second being called the  "far
  78.         heap".   The "heap" is an area within the 64K boundary  that
  79.         can  store dynamically allocated date and the "far heap"  is
  80.         an  area outside of this 64K boundary which can be  accessed
  81.         by the program to store data and variables.
  82.  
  83.              The  data  and variables are put on the "heap"  by  the
  84.         system  as  calls to "malloc" are made.   The  system  keeps
  85.         track  of where the data is stored.  Data and variables  can
  86.         be deallocated as desired leading to holes in the heap.  The
  87.         system  knows  where  the holes are and will  use  them  for
  88.         additional  data  storage as more "malloc" calls  are  made.
  89.         The  structure  of  the heap is  therefore  a  very  dynamic
  90.         entity, changing constantly.
  91.  
  92.              The  data  and variables are put on the "far  heap"  by
  93.         utilizing  calls  to  "farmalloc",  "farcalloc",  etc.   and
  94.         removed  through use of the function "farfree".  Study  your
  95.         Turbo  C  Reference Guide for details of how  to  use  these
  96.         features.
  97.  
  98.                             MORE ABOUT SEGMENTS
  99.  
  100.              Turbo  C  gives the user a choice of memory  models  to
  101.         use.  The  user  has a choice of using a model  with  a  64K
  102.         limitation  for  either program or data leading to  a  small
  103.         fast  program or selecting a 640K limitation  and  requiring
  104.         longer  address calls leading to less efficient  addressing.
  105.         Using  the  larger  address  space  requires  inter  segment
  106.         addressing,  resulting in the slightly slower running  time.
  107.         The  time  is probably insignificant in most  programs,  but
  108.         there are other considerations.
  109.  
  110.              If a program uses no more than 64K bytes for the  total
  111.         of its code and memory and if it doesn't use a stack, it can
  112.         be  made  into  a  .COM file.  With Turbo  C  this  is  only
  113.         possible by using the tiny memory model.  Since a .COM  file
  114.         is  already in a memory image format, it can be loaded  very
  115.         quickly  whereas  a  file in an .EXE format  must  have  its
  116.         addresses  relocated  as  it is loaded.   Therefore  a  tiny
  117.         memory  model can generate a program that loads faster  than
  118.         one  generated with a larger memory model.  Don't  let  this
  119.         worry  you,  it is a fine point that few  programmers  worry
  120.         about.
  121.  
  122.  
  123.  
  124.                                   Page 88
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 12 - Dynamic Allocation
  135.  
  136.  
  137.              Even  more  important than the need to stay within  the
  138.         small memory model is the need to stay within the  computer.
  139.         If  you  had a program that used several large data  storage
  140.         areas,  but not at the same time,  you could load one  block
  141.         storing  it  dynamically,  then get rid of it and reuse  the
  142.         space for the next large block of data.  Dynamically storing
  143.         each block of data in succession, and using the same storage
  144.         for  each block may allow you to run your entire program  in
  145.         the computer without breaking it up into smaller programs.
  146.  
  147.                        BACK TO THE "MALLOC" FUNCTION
  148.  
  149.              Hopefully  the above description of the "heap" and  the
  150.         overall plan for dynamic allocation helped you to understand
  151.         what  we are doing with the "malloc"  function.   It  simply
  152.         asks the system for a block of memory of the size specified,
  153.         and  gets  the block with the pointer pointing to the  first
  154.         element of the block.   The only argument in the parentheses
  155.         is the size of the block desired and in our present case, we
  156.         desire  a  block  that will hold one of  the  structures  we
  157.         defined at the beginning of the program.  The "sizeof" is  a
  158.         new  function, new to us at least, that returns the size  in
  159.         bytes of the argument within its parentheses.  It therefore,
  160.         returns the size of the structure named "animal", in  bytes,
  161.         and  that  number is sent to the system  with  the  "malloc"
  162.         call.   At the completion of that call, we have a  block  on
  163.         the heap allocated to us, with "pet1" pointing to the  block
  164.         of data.
  165.  
  166.                               WHAT IS A CAST?
  167.  
  168.              We  still  have  a  funny  looking  construct  at   the
  169.         beginning  of the "malloc" function call.   That is called a
  170.         "cast".   The  "malloc"  function returns a block  with  the
  171.         pointer  pointing  to it being a pointer of type  "char"  by
  172.         default.  Many times, if not most, you do not want a pointer
  173.         to a "char" type variable,  but to some other type.  You can
  174.         define  the  pointer type with the construct  given  on  the
  175.         example line.   In this case we want the pointer to point to
  176.         a  structure of type "animal",  so we tell the compiler with
  177.         this strange looking construct.   Even if you omit the cast,
  178.         most compilers will return a pointer correctly,  give you  a
  179.         warning,  and  go  on to produce a working program.   It  is
  180.         better programming practice to provide the compiler with the
  181.         cast to prevent getting the warning message.
  182.  
  183.                 USING THE DYNAMICALLY ALLOCATED MEMORY BLOCK
  184.  
  185.              If you remember our studies of structures and pointers,
  186.         you  will recall that if we have a structure with a  pointer
  187.         pointing  to it,  we can access any of the variables  within
  188.  
  189.  
  190.                                   Page 89
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                       Chapter 12 - Dynamic Allocation
  201.  
  202.  
  203.         the structure.   In the next three lines of the program,  we
  204.         assign  some silly data to the structure  for  illustration.
  205.         It  should come as no surprise to you that these  assignment
  206.         statements  look just like assignments to statically defined
  207.         variables.
  208.  
  209.  
  210.              In the next statement, we assign the value of "pet1" to
  211.         "pet2" also.   This creates no new data,  we simply have two
  212.         pointers  to the same object.   Since "pet2" is pointing  to
  213.         the structure we created above,  "pet1" can be reused to get
  214.         another  dynamically allocated structure which is just  what
  215.         we  do next.   Keep in mind that "pet2" could have  just  as
  216.         easily been used for the new allocation.   The new structure
  217.         is filled with silly data for illustration.
  218.  
  219.              Finally,  we  allocate another block on the heap  using
  220.         the  pointer  "pet3",  and fill its block with  illustrative
  221.         data.
  222.  
  223.              Printing  the  data out should pose no problem  to  you
  224.         since  there is nothing new in the three  print  statements.
  225.         It is left for you to study.
  226.  
  227.              Even though it is not illustrated in this tutorial, you
  228.         can dynamically allocate and use simple variables such as  a
  229.         single  "char"  type variable.  This should  be  discouraged
  230.         however,  since it would be very inefficient.  This is  only
  231.         mentioned  to point out that there is nothing magic about  a
  232.         data  structure  that  would  allow  it  to  be  dynamically
  233.         allocated while simple types could not.
  234.  
  235.                GETTING RID OF THE DYNAMICALLY ALLOCATED DATA
  236.  
  237.              Another new function is used to get rid of the data and
  238.         free  up  the  space on the heap  for  reuse,  the  function
  239.         "free".   To use it,  you simply call it with the pointer to
  240.         the   block  as  the  only  argument,   and  the  block   is
  241.         deallocated.
  242.  
  243.              In  order  to illustrate another aspect of the  dynamic
  244.         allocation and deallocation of data,  an additional step  is
  245.         included in the program on your monitor.  The pointer "pet1"
  246.         is assigned the value of "pet3" in line 38.  In doing  this,
  247.         the  block that "pet1" was pointing to is  effectively  lost
  248.         since  there  is  no pointer that is now  pointing  to  that
  249.         block.   It  can  therefore  never  again  be  referred  to,
  250.         changed,  or disposed of.  That memory, which is a block  on
  251.         the  heap,  is  wasted  from this point  on.   This  is  not
  252.         something that you would ever purposely do in a program.  It
  253.         is only done here for illustration.
  254.  
  255.  
  256.                                   Page 90
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                       Chapter 12 - Dynamic Allocation
  267.  
  268.  
  269.  
  270.              The  first  "free" function call removes the  block  of
  271.         data that "pet1" and "pet3" were pointing to, and the second
  272.         "free"  call  removes  the block of  data  that  "pet2"  was
  273.         pointing  to.   We therefore have lost access to all of  our
  274.         data  generated earlier.   There is still one block of  data
  275.         that  is on the heap but there is no pointer to it since  we
  276.         lost  the address to it.   Trying to "free" the data pointed
  277.         to by "pet1" would result in an error because it has already
  278.         been  "freed"  by the use of "pet3".  There is  no  need  to
  279.         worry,  when  we  return to DOS, the  entire  heap  will  be
  280.         disposed  of with no regard to what we have put on it.   The
  281.         point  does  need to made that, if you lose a pointer  to  a
  282.         block  of  the heap, it forever removes that block  of  data
  283.         storage from our use and we may need that storage later.
  284.  
  285.              Compile and run the program to see if it does what  you
  286.         think it should do based on this discussion.
  287.  
  288.                         THAT WAS A LOT OF DISCUSSION
  289.  
  290.              It took nearly four pages to get through the discussion
  291.         of  the last program but it was time well spent.   It should
  292.         be  somewhat exciting to you to know that there  is  nothing
  293.         else to learn about dynamic allocation,  the last four pages
  294.         covered  it all.   Of course,  there is a lot to learn about
  295.         the  technique  of using dynamic allocation,  and  for  that
  296.         reason,  there  are two more files to study.   But the  fact
  297.         remains,  there  is  nothing  more to  learn  about  dynamic
  298.         allocation than what was given so far in this chapter.
  299.  
  300.                             AN ARRAY OF POINTERS
  301.  
  302.              Load and display the file BIGDYNL.C for another example
  303.         of dynamic allocation.   This program is very similar to the
  304.         last one since we use the same structure,  but this time  we
  305.         define an array of pointers to illustrate the means by which
  306.         you  could build a large database using an array of pointers
  307.         rather  than a single pointer to each element.   To keep  it
  308.         simple  we  define  12 elements in  the  array  and  another
  309.         working pointer named "point".
  310.  
  311.              The "*pet[12]" is new to you so a few words would be in
  312.         order.  What we have defined is an array of 12 pointers, the
  313.         first  being "pet[0]",  and the last  "pet[11]".   Actually,
  314.         since an array is itself a pointer, the name "pet" by itself
  315.         is a pointer to a pointer.   This is valid in C, and in fact
  316.         you  can  go  farther  if needed but you  will  get  quickly
  317.         confused.   I  know  of no limit as to how  many  levels  of
  318.         pointing are possible,  so a definition such as "int ****pt"
  319.         is legal as a pointer to a pointer to a pointer to a pointer
  320.  
  321.  
  322.                                   Page 91
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                       Chapter 12 - Dynamic Allocation
  333.  
  334.  
  335.         to an integer type variable, if I counted right.  Such usage
  336.         is discouraged until you gain considerable experience.
  337.  
  338.              Now that we have 12 pointers which can be used like any
  339.         other  pointer,  it  is a simple matter to write a  loop  to
  340.         allocate  a data block dynamically for each and to fill  the
  341.         respective  fields with any data desirable.   In this  case,
  342.         the  fields  are  filled with simple data  for  illustrative
  343.         purposes,  but we could be reading in a  database,  readings
  344.         from some test equipment, or any other source of data.
  345.  
  346.              A  few fields are randomly picked to receive other data
  347.         to illustrate that simple assignments can be used,  and  the
  348.         data is printed out to the monitor.   The pointer "point" is
  349.         used  in the printout loop only to serve as an illustration,
  350.         the  data could have been easily printed using the  "pet[n]"
  351.         means  of definition.   Finally,  all 12 blocks of data  are
  352.         freed before terminating the program.
  353.  
  354.              Compile  and run this program to aid  in  understanding
  355.         this  technique.   As stated earlier,  there was nothing new
  356.         here  about  dynamic  allocation,  only about  an  array  of
  357.         pointers.
  358.  
  359.                                A LINKED LIST
  360.  
  361.              We  finally  come to the grandaddy of  all  programming
  362.         techniques as far as being intimidating.   Load the  program
  363.         DYNLINK.C  for an example of a dynamically allocated  linked
  364.         list.   It  sounds terrible,  but after a little time  spent
  365.         with it,  you will see that it is simply another programming
  366.         technique  made  up  of  simple components  that  can  be  a
  367.         powerful tool.
  368.  
  369.              In order to set your mind at ease,  consider the linked
  370.         list  you used when you were a child.   Your sister gave you
  371.         your birthday present,  and when you opened it,  you found a
  372.         note that said,  "Look in the hall closet."  You went to the
  373.         hall closet,  and found another note that said, "Look behind
  374.         the  TV  set."  Behind the TV you found  another  note  that
  375.         said,  "Look  under  the  coffee pot."  You  continued  this
  376.         search,  and finally you found your pair of socks under  the
  377.         dogs  feeding dish.   What you actually did was to execute a
  378.         linked  list,  the starting point being the wrapped  present
  379.         and the ending point being under the dogs feeding dish.  The
  380.         list ended at the dogs feeding dish since there were no more
  381.         notes.
  382.  
  383.              In  the program DYNLINK.C,  we will be doing  the  same
  384.         thing as your sister forced you to do.   We will however, do
  385.         it  much  faster and we will leave a little pile of data  at
  386.  
  387.  
  388.                                   Page 92
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                       Chapter 12 - Dynamic Allocation
  399.  
  400.  
  401.         each of the intermediate points along the way.  We will also
  402.         have   the  capability  to  return  to  the  beginning   and
  403.         retraverse the entire list again and again if we so desire.
  404.  
  405.                             THE DATA DEFINITIONS
  406.  
  407.              This program starts similarly to the last two with  the
  408.         addition  of the definition of a constant to be used  later.
  409.         The  structure  is nearly the same as that used in the  last
  410.         two programs except for the addition of another field within
  411.         the  structure in line 11, the pointer.  This pointer  is  a
  412.         pointer  to another structure of this same type and will  be
  413.         used  to point to the next structure in order.  To  continue
  414.         the above analogy, this pointer will point to the next note,
  415.         which in turn will contain a pointer to the next note  after
  416.         that.
  417.  
  418.              We  define three pointers to this structure for use  in
  419.         the program, and one integer to be used as a counter, and we
  420.         are ready to begin using the defined structure for  whatever
  421.         purpose  we  desire.   In  this case,  we  will  once  again
  422.         generate nonsense data for illustrative purposes.
  423.  
  424.                               THE FIRST FIELD
  425.  
  426.              Using  the  "malloc" function,  we request a  block  of
  427.         storage on the "heap" and fill it with data.  The additional
  428.         field in this example, the pointer, is assigned the value of
  429.         NULL, which is only used to indicate that this is the end of
  430.         the  list.   We  will  leave  the pointer  "start"  at  this
  431.         structure,  so  that  it  will always  point  to  the  first
  432.         structure of the list.   We also assign "prior" the value of
  433.         "start" for reasons we will see soon.  Keep in mind that the
  434.         end  points of a linked list will always have to be  handled
  435.         differently  than those in the middle of a list.   We have a
  436.         single  element  of  our  list now and  it  is  filled  with
  437.         representative data.
  438.  
  439.                        FILLING ADDITIONAL STRUCTURES
  440.  
  441.              The  next group of assignments and  control  statements
  442.         are  included  within a "for" loop so we can build our  list
  443.         fast  once  it is defined.   We will go through the  loop  a
  444.         number  of times equal to the constant "RECORDS" defined  at
  445.         the  beginning  of  our  program.   Each  time  through,  we
  446.         allocate memory,  fill the first three fields with nonsense,
  447.         and  fill the pointers.   The pointer in the last record  is
  448.         given  the  address of this new record because  the  "prior"
  449.         pointer is pointing to the prior record.  Thus "prior->next"
  450.         is given the address of the new record we have just  filled.
  451.         The  pointer in the new record is assigned the value "NULL",
  452.  
  453.  
  454.                                   Page 93
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                       Chapter 12 - Dynamic Allocation
  465.  
  466.  
  467.         and  the  pointer "prior" is given the address of  this  new
  468.         record  because the next time we create a record,  this  one
  469.         will  be  the  prior  one at  that  time.   That  may  sound
  470.         confusing  but it really does make sense if you  spend  some
  471.         time studying it.
  472.  
  473.              When  we have gone through the "for" loop 6  times,  we
  474.         will  have  a  list  of 7 structures including  the  one  we
  475.  
  476.         generated  prior  to  the loop.   The  list  will  have  the
  477.         following characteristics.
  478.  
  479.  
  480.         1. "start" points to the first structure in the list.
  481.  
  482.         2. Each structure contains a pointer to the next structure.
  483.  
  484.         3. The last structure has a pointer  that points to NULL and
  485.            can be used to detect the end.
  486.  
  487.            start->struct1              This diagram should aid in
  488.                   name              understanding the structure of
  489.                   breed             the data at this point.
  490.                   age
  491.                   point->struct2
  492.                          name
  493.                          breed
  494.                          age
  495.                          point->struct3
  496.                                 name
  497.                                 breed
  498.                                 age
  499.                                 point-> . . . . struct7
  500.                                                 name
  501.                                                 breed
  502.                                                 age
  503.                                                 point->NULL
  504.  
  505.  
  506.              It should be clear to you,  if you understand the above
  507.         structure,  that  it is not possible to simply jump into the
  508.         middle of the structure and change a few values.   The  only
  509.         way  to  get  to the third structure is by starting  at  the
  510.         beginning  and working your way down through  the  structure
  511.         one  record at a time.   Although this may seem like a large
  512.         price  to  pay for the convenience of putting so  much  data
  513.         outside of the program area,  it is actually a very good way
  514.         to store some kinds of data.
  515.  
  516.             A  word processor would be a good application  for  this
  517.         type  of data structure because you would never need to have
  518.  
  519.  
  520.                                   Page 94
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                       Chapter 12 - Dynamic Allocation
  531.  
  532.  
  533.         random access to the data.   In actual practice, this is the
  534.         basic type of storage used for the text in a word  processor
  535.         with one line of text per record.   Actually, a program with
  536.         any degree of sophistication would use a doubly linked list.
  537.         This  would  be  a list with two pointers  per  record,  one
  538.         pointing down to the next record,  and the other pointing up
  539.         to the record just prior to the one in question.  Using this
  540.         kind  of a record structure would allow traversing the  data
  541.         in either direction.
  542.  
  543.                            PRINTING THE DATA OUT
  544.  
  545.              To print the data out, a similar method is used as that
  546.         used to generate the data.  The pointers are initialized and
  547.         are  then  used  to go from record  to  record  reading  and
  548.         displaying   each  record  one  at  a  time.    Printing  is
  549.         terminated when the NULL on the last record is found, so the
  550.         program  doesn't even need to know how many records  are  in
  551.         the list.   Finally, the entire list is deleted to make room
  552.         in  memory  for any additional data that may be  needed,  in
  553.         this case, none.  Care must be taken to assure that the last
  554.         record is not deleted before the NULL is checked.   Once the
  555.         data is gone,  it is impossible to know if you are  finished
  556.         yet.
  557.  
  558.                MORE ABOUT DYNAMIC ALLOCATION AND LINKED LISTS
  559.  
  560.              It  is  not difficult,  and it is not trivial,  to  add
  561.         elements into the middle of a linked lists.  It is necessary
  562.         to create the new record,  fill it with data,  and point its
  563.         pointer to the record it is desired to precede.   If the new
  564.         record  is  to be installed between the  3rd  and  4th,  for
  565.         example,  it is necessary for the new record to point to the
  566.         4th record,  and the pointer in the 3rd record must point to
  567.         the new one.  Adding a new record to the beginning or end of
  568.         a  list are each special cases.   Consider what must be done
  569.         to add a new record in a doubly linked list.
  570.  
  571.              Entire books are written describing different types  of
  572.         linked lists and how to use them,  so no further detail will
  573.         be  given.   The amount of detail given should be sufficient
  574.         for a beginning understanding of C and its capabilities.
  575.  
  576.                        ANOTHER NEW FUNCTION - CALLOC
  577.  
  578.              One  more  function must  be  mentioned,  the  "calloc"
  579.         function.   This  function  allocates a block of memory  and
  580.         clears  it  to  all  zeros  which  may  be  useful  in  some
  581.         circumstances.   It is similar to "malloc" and will be  left
  582.         as an exercise for you to read about and use "calloc" if you
  583.         desire.
  584.  
  585.  
  586.                                   Page 95
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                       Chapter 12 - Dynamic Allocation
  597.  
  598.  
  599.  
  600.         PROGRAMMING EXERCISES
  601.  
  602.         1.  Rewrite the example program STRUCT1.C from chapter 11 to
  603.             dynamically allocate the two structures.
  604.  
  605.         2.  Rewrite the example program STRUCT2.C from chapter 11 to
  606.             dynamically allocate the 12 structures.
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.                                   Page 96
  653.